package smcms.service.sso.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class AccountResp implements UserDetails {

    private String username;
    private String password;
    private List<String> permission;
    private List<CloudStoreInfo> cloudStore;
    private String cloudId;
    private String staffName;

    public void setPermission(List<String> permission) {
        this.permission = permission;
    }

    private Collection<? extends GrantedAuthority> authorities;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List <GrantedAuthority> authorityList = new ArrayList<>();
        permission.forEach(p -> {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(p);
            authorityList.add(authority);
        });
        return authorityList;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String toString() {
        return "AccountResp{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", permission=" + permission +
                ", cloudId='" + cloudId + '\'' +
                ", authorities=" + authorities +
                '}';
    }
}
